package com.sondon.mayi.activity;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;
import org.apache.http.Header;

import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.loopj.android.http.TextHttpResponseHandler;
import com.sondon.mayi.configs.URLConfigs;
import com.sondon.mayi.entity.AddressInfo;
import com.sondon.mayi.entity.RespondBaseEntity;
import com.sondon.mayi.fruit.R;
import com.sondon.mayi.ui.MyMultipleTopBar;
import com.sondon.mayi.ui.TopBarClickListener;
import com.sondon.mayi.util.AsyncHttpUtil;
import com.sondon.mayi.util.GsonUtil;
import com.sondon.mayi.util.LogUtil;
import com.sondon.mayi.util.ToastUtils;

/**
 * @Project : Fruit
 * @Package : com.sondon.mayi.activity
 * @Class : AddressShowActivity
 * @Author : 蔡文锋
 * @DateTime：2015年6月14日 下午3:11:32
 * @Blog：http://blog.csdn.net/caiwenfeng_for_23
 * @Description : { 显示地址详细地址 }
 *
 */
@EActivity(R.layout.address_show)
public class AddressShowActivity extends BaseActivity {
	@ViewById
	MyMultipleTopBar topBar;
	@ViewById
	TextView receiver;//收货人
	@ViewById
	TextView mobile;//手机号
	@ViewById
	TextView provice_city;//省市
	@ViewById
	TextView address;//地址
	@ViewById
	TextView zipcode;//邮编
	@ViewById
	Button remove;//删除地址
	@ViewById
	Button set_default;//设置为默认
	
	public AddressInfo addressInfo;
	
	@AfterViews
	public void initView(){
		topBar.setTopBarClickListener(new TopBarClickListener() {
			
			@Override
			public void rightBtnClick() {
				
			}
			
			@Override
			public void leftBtnClick() {
				onBackPressed();
			}
		});
		
		addressInfo=(AddressInfo) getIntent().getExtras().getSerializable(AddressManagerActivity.ADDRESSINFO);
		if(addressInfo !=null ){
			receiver.setText(addressInfo.getAccept_name());
			mobile.setText(addressInfo.getMobile());
			
			String city=addressInfo.getCity_name();
			if("市辖区".equals(city)||"县".equals(city)){
				city="";
			}
			provice_city.setText(
					addressInfo.getProvince_name()
					+city
					+addressInfo.getArea_name()
					);
			address.setText(addressInfo.getAddress());
			zipcode.setText(addressInfo.getZip());
		}
	}
	
 	@Click({R.id.remove,R.id.set_default})
	public void onClick(View v){
		switch (v.getId()) {
		case R.id.remove://删除地址
			AsyncHttpUtil.get(URLConfigs.DelAddress_ukey+myprefs.Ukey().get()+"&id="+addressInfo.getId(),
					null, new TextHttpResponseHandler() {
						
						@Override
						public void onSuccess(int status, Header[] arg1, String json) {
							if(status==200){
								LogUtil.e(TAG, "Address Del  json :"+json);
								RespondBaseEntity entity=GsonUtil.GetFromJson(json, RespondBaseEntity.class);
								
								if(entity.isStatus()){
//									openActivity(AddressManagerActivity_.class);
//									AddressShowActivity.this.finish();
									onBackPressed();
								}else{
									ToastUtils.show(context, entity.getMsg());
								}
								
								
							}
						}
						
						@Override
						public void onFailure(int arg0, Header[] arg1, String arg2, Throwable arg3) {
							ToastUtils.show(context, R.string.network_unavailable);
						}
					});
			
			break;
		case R.id.set_default://设为默认
			AsyncHttpUtil.get(URLConfigs.SetDefaultAddress_ukey+myprefs.Ukey().get()+"&id="+addressInfo.getId(),
					null, new TextHttpResponseHandler() {
						
						@Override
						public void onSuccess(int status, Header[] arg1, String json) {
							if(status==200){
								LogUtil.e(TAG, "Address Default  json :"+json);
								RespondBaseEntity entity=GsonUtil.GetFromJson(json, RespondBaseEntity.class);
								
								if(entity.isStatus()){
//									openActivity(AddressManagerActivity_.class);
//									AddressShowActivity.this.finish();
									onBackPressed();
								}else{
									ToastUtils.show(context, entity.getMsg());
								}
								
							}
						}
						
						@Override
						public void onFailure(int arg0, Header[] arg1, String arg2, Throwable arg3) {
							ToastUtils.show(context, R.string.network_unavailable);
						}
					});
			break;
		default:
			break;
		}
	}
	
}
